<template>
  <div class="comments">
    <hmheader title="精彩评论">
      <span
        slot="left"
        class="iconfont iconjiantou2"
        @click="$router.back()"
      ></span>
    </hmheader>
    <div class="lists" v-for="item in parent" :key="item.id">
      <div class="item">
        <div class="head">
          <img :src="item.user.head_img" alt />
          <div>
            <p>{{ item.user.nickname }}</p>
            <span>{{ item.create_date | offsetTimeFormat }}</span>
          </div>
          <span @click="replyComment(item)">回复</span>
        </div>
        <!-- 第二层渲染 -->
        <reply
          v-if="item.parent"
          :parent="item.parent"
          @send="replyComment"
        ></reply>
        <div class="text">{{ item.content }}</div>
      </div>
    </div>
    <!-- 评论 -->
    <commentFooter
      :post="post"
      @refresh="refresh"
      :item="item"
      @reset="item = null"
    ></commentFooter>
  </div>
</template>

<script>
// 引入回复组件
import reply from "@/components/reply";
import commentFooter from "@/components/commentFooter";
import hmheader from "@/components/hmheader";
// 相对时间过滤器
import { offsetTimeFormat } from "@/utils/filters";
import axios from "@/utils/myaxios";
import { commentlist, getPostDetail } from "@/apis/post";
export default {
  data() {
    return {
      parent: {},
      post: {},
      item: "",
    };
  },
  // 注册过滤器
  filters: {
    offsetTimeFormat,
  },
  // 注册组件
  components: {
    hmheader,
    commentFooter,
    reply,
  },
  activated() {
    // console.log(this.$route.params.id);
    this.init();
  },
  methods: {
    async init() {
      let res = await commentlist(this.$route.params.id);
      // console.log(res);
      this.parent = res.data.data;
      // 改造数据
      this.parent = this.parent.map((v) => {
        v.user.head_img = axios.defaults.baseURL + v.user.head_img;
        return v;
      });
      // console.log(axios.defaults.baseURL);
      console.log(this.parent);

      // 评论
      let res2 = await getPostDetail(this.$route.params.id);
      // console.log(res2.data.data);
      this.post = res2.data.data;
    },
    refresh() {
      // 刷新页面
      this.init();
      //滚动到顶部
      window.scrollTo(0, 0);
    },
    // 回复评论
    replyComment(item) {
      console.log(item);
      this.item = item;
    },
  },
};
</script>

<style lang='less' scoped>
.lists {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
}
</style>